package com.ukao.pad.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.ukao.pad.R;
import com.ukao.pad.base.BaseDialogFragment;
import com.ukao.pad.bean.UserInfoBean;
import com.ukao.pad.rfid.UserInfoEvent;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.SystemUIUtils;
import com.ukao.pad.utils.T;
import com.ukao.pad.widget.StateButton;
import com.ukao.pad.widget.StateImageView;

import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 *
 * Created by ukaodev on 2017/6/2.
 */

public class VipCardDialogFragment extends BaseDialogFragment {

    Unbinder unbinder;
    @BindView(R.id.back_btn)
    StateButton backBtn;
    @BindView(R.id.include_hide_layout)
    LinearLayout includeHideLayout;
    @BindView(R.id.close_btn1)
    StateImageView closeBtn1;
    @BindView(R.id.title1)
    TextView title1;
    @BindView(R.id.binding_btn)
    StateButton bindingBtn;
    @BindView(R.id.title_unbind_btn)
    StateButton titleUnbindBtn;
    @BindView(R.id.loss_btn)
    StateButton lossBtn;
    @BindView(R.id.show_card_layout)
    RelativeLayout showCardLayout;
    @BindView(R.id.use_card_name)
    TextView useCardName;
    @BindView(R.id.use_card_lever)
    TextView useCardLever;
    @BindView(R.id.use_card_no)
    TextView useCardNo;

    @BindView(R.id.textView9)
    TextView textView9;
    @BindView(R.id.bing_txt)
    TextView bingTxt;
    @BindView(R.id.no_bing_txt)
    TextView noBingTxt;
    @BindView(R.id.use_card_name1)
    TextView useCardName1;
    @BindView(R.id.use_card_lever1)
    TextView useCardLever1;
    @BindView(R.id.use_card_no1)
    TextView useCardNo1;
    @BindView(R.id.use_card_discount1)
    TextView useCardDiscount1;
    @BindView(R.id.textView90)
    TextView textView90;
    @BindView(R.id.bing_layout)
    RelativeLayout bingLayout;
    @BindView(R.id.card_layout)
    RelativeLayout cardLayout;
    @BindView(R.id.include_card_layout)
    LinearLayout includeCardLayout;
    @BindView(R.id.hide_txt)
    TextView hideTxt;
    @BindView(R.id.sure_unbind_btn)
    StateButton sureUnbindBtn;
    @BindView(R.id.cardno_bing_btn)
    StateButton cardnoBingBtn;
    @BindView(R.id.use_card_discount)
    TextView useCardDiscount;
    @BindView(R.id.rfidNo)
    EditText rfidNo;
    @BindView(R.id.checkCode)
    EditText checkCode;
    @BindView(R.id.cancel_code_btn)
    StateButton cancelCodeBtn;
    @BindView(R.id.sure_code_btn)
    StateButton sureCodeBtn;
    @BindView(R.id.include_code_layout)
    LinearLayout includeCodeLayout;


    private boolean binding;
    private boolean isBingding;
    private UserInfoBean.UserBean.CardBean mCardBean;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            isBingding = getArguments().getBoolean("binding");
            mCardBean =  getArguments().getParcelable("cardBean");
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_vip_card_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getDialog()!=null){
            SystemUIUtils.setStickFullScreen(getDialog().getWindow());
        }
    }

    private void init() {
        title1.setText("会员卡");
        includeCodeLayout.setVisibility(View.GONE);
        if (isBingding) {
            bingTxt.setText("已绑定");
            cardnoBingBtn.setVisibility(View.GONE);
            titleUnbindBtn.setVisibility(View.VISIBLE);
            bindingBtn.setVisibility(View.GONE);
            lossBtn.setVisibility(View.VISIBLE);
            noBingTxt.setVisibility(View.GONE);
            bingLayout.setVisibility(View.VISIBLE);
            cardLayout.setBackgroundResource(R.drawable.dialog_card_entity);
            bingTxt.setTextColor(resources.getColor(R.color.green));
        } else {
            titleUnbindBtn.setVisibility(View.GONE);
            includeCardLayout.setVisibility(View.VISIBLE);
            lossBtn.setVisibility(View.GONE);
            noBingTxt.setVisibility(View.VISIBLE);
            bingLayout.setVisibility(View.INVISIBLE);
            bingTxt.setText("未绑定");
            bingTxt.setTextColor(resources.getColor(R.color.warm_red));
            useCardName.setText(mCardBean.getNo());
        }
        useCardName.setText(mCardBean.getInfo().getName());
        useCardName1.setText(mCardBean.getInfo().getName());
        useCardNo.setText(mCardBean.getNo());
        useCardNo1.setText(mCardBean.getNo());
        useCardDiscount.setText(mCardBean.getInfo().getDiscount().replace("折", ""));
        useCardLever.setText("卡等级:" + mCardBean.getInfo().getLevel());
        useCardNo.setText(mCardBean.getNo());
        useCardDiscount1.setText(mCardBean.getInfo().getDiscount().replace("折", ""));
        useCardLever1.setText("卡等级:" + mCardBean.getInfo().getLevel());
        useCardNo1.setText(mCardBean.getNo());
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    protected boolean useEventBus() {
        return false;
    }

    @Subscribe
    public void onEvent(UserInfoEvent event) {

    }


    @OnClick({R.id.binding_btn, R.id.loss_btn, R.id.close_btn1, R.id.back_btn, R.id.title_unbind_btn, R.id.sure_unbind_btn,
            R.id.cardno_bing_btn,R.id.sure_code_btn,R.id.cancel_code_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.loss_btn:
                title1.setText("挂失作废会员卡");
                includeHideLayout.setVisibility(View.VISIBLE);
                includeCardLayout.setVisibility(View.GONE);
                sureUnbindBtn.setText("确认作废");
                title1.setText("挂失作废会员卡");
                hideTxt.setText("挂失作废后此卡件无法使用");
                break;
            case R.id.close_btn1:
                dismiss();
                break;
            case R.id.back_btn:
                includeHideLayout.setVisibility(View.GONE);
                includeCardLayout.setVisibility(View.VISIBLE);
                break;
            case R.id.title_unbind_btn:/**/
                hideTxt.setText("解绑后该客户将无法使用此卡");
                includeHideLayout.setVisibility(View.VISIBLE);
                includeCardLayout.setVisibility(View.GONE);
                sureUnbindBtn.setText("确定解绑");
                break;
            case R.id.sure_unbind_btn:   ///确定解绑
                String temgString = sureUnbindBtn.getText().toString().toString().trim();
                switch (temgString) {
                    case "确定解绑":
                        UserInfoEvent.postNoData(UserInfoEvent.Message.UNBING);
                        dismiss();
                        break;
                    case "确认作废":
                        UserInfoEvent.postNoData(UserInfoEvent.Message.LOSSCARD);
                        dismiss();
                        break;
                }

                break;
            case R.id.cardno_bing_btn:  /*验证绑定*/
                 includeCardLayout.setVisibility(View.GONE);
                includeCodeLayout.setVisibility(View.VISIBLE);
                title1.setText("卡号验证码绑卡");

                break;

            case R.id.sure_code_btn:  /*确定验证绑定*/
                     String strRfidNo =getText(rfidNo);
                String   strCheckCode=getText(checkCode);
                if (CheckUtils.isEmpty(strRfidNo)){

                    return;
                } else if (CheckUtils.isEmpty(strCheckCode)&&strCheckCode.length()<3){
                    T.show("请输入验证码");
                    return;
                }
                Map   mMap=new HashMap();
                mMap.put(ARG_PARAM1, strRfidNo);
                mMap.put(ARG_PARAM2,strCheckCode);
                UserInfoEvent.postHasData(UserInfoEvent.Message.CODEBING,mMap);
                break;
            case R.id.cancel_code_btn:
                dismiss();
                break;
            case R.id.binding_btn: /*绑卡*/
                UserInfoEvent.postNoData(UserInfoEvent.Message.BING);
              //  dismiss();
                break;


        }
    }

}
